//
//  RestaurantApi.swift
//  FeastBooking
//
//  Created by Imon on 2017/9/23.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import Moya

enum RestaurantApi {
    case getRestaurants(page: Int, sort: String, minRating: String, minPrice: String, maxPrice: String, name: String)
    case getRestaurant(id: Int)
    case getRestaurantRating(id: Int)
    case rateRestaurant(id: Int, score: Int)
    case getBanquetHall(id: Int)
    case getFeast(id: Int)
    case getReviews(id: Int)
    case getDiscounts(hotelId: Int, since: String)
}

extension RestaurantApi: TargetType {
    
    var baseURL: URL {
        return URL(string: serverBasePath)!
    }
    
    var path: String {
        switch self {
        case .getRestaurants:
            return "/hotels"
        case .getRestaurant(let id):
            return "/hotels/\(id)"
        case .getRestaurantRating(let id):
            return "/hotels/\(id)/rating"
        case .rateRestaurant(let id, _):
            return "/hotels/\(id)/rating"
        case .getBanquetHall(let id):
            return "/banquet_halls/\(id)"
        case .getFeast(let id):
            return "/feasts/\(id)"
        case .getReviews(let id):
            return "/hotels/\(id)/reviews"
        case .getDiscounts(let hotelId, _):
            return "/hotels/\(hotelId)/discounts"
        }
    }
    
    var method: Moya.Method {
        switch self {
    case .getRestaurants, .getRestaurant, .getRestaurantRating, .getBanquetHall, .getFeast, .getReviews, .getDiscounts:
            return .get
        case .rateRestaurant:
            return .post
        }
    }
    
    var parameters: [String: Any]? {
        switch self {
        case .getRestaurants(let page, let sort, let minRating, let minPrice, let maxPrice, let name):
            return ["page": page, "sort": sort, "min_rating": minRating, "min_price": minPrice, "max_price": maxPrice, "name": name]
        case .rateRestaurant(_, let score):
            return ["score": score]
        case .getDiscounts(_, let since):
            return ["since": since]
        default:
            return nil
        }
    }
    
    var task: Task {
        return .request
    }
    
    var sampleData: Data {
        return "{\"data\":{\"msg\":\"good\"},\"meta\":{\"status\":200,\"msg\":\"OK\"}}".data(using: String.Encoding.utf8)!
    }
    
    var parameterEncoding: ParameterEncoding {
        switch self {
        case .rateRestaurant:
            return JSONEncoding.default
        case .getRestaurants, .getDiscounts:
            return URLEncoding.queryString
        default:
            return URLEncoding.default
        }
    }
}
